﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EkStudio.com.common.unit;
using EkStudio.com.bean.xml.ProjectMenu;
using DevExpress.XtraEditors.Controls;
using EkStudio.com.bean.xml.RootInterface;
using EkStudio.com.bean.xml.RootInterface.DataSource;

namespace EkStudio.com.ui.form.Auto
{
    public partial class AutoQuerySelect : DevExpress.XtraEditors.XtraForm
    {

        private ProjectRootPanel RootPanel = null;

        private List<SelectItem> formatModList = new List<SelectItem>();



        public AutoQuerySelect(ProjectRootPanel root)
        {
            InitializeComponent();
            RootPanel = root;
            this.InitForm();
        }



        private void InitForm()
        {
            ProjectMenuItem menu = RootPanel.MenuItem;
            ListBoxPop.Items.Clear();
            //加载字典
            if (menu.Dictionary != null && menu.Dictionary.Module != null && menu.Dictionary.Module.Count > 0)
            {
                for (int i = 0; i < menu.Dictionary.Module.Count; i++)
                {
                    ProjectMenuCommonElement it = menu.Dictionary.Module[i];
                    SelectItem item = new SelectItem(it.Command, it.Caption, ProjectUtil.TYPE_DIC_MODULE,"");
                    CheckedListBoxItem Lit = new CheckedListBoxItem(item, false);
                    ListBoxPop.Items.Add(Lit);
                }
            }
            //加载模块
            if (menu.Business != null && menu.Business.Modules != null && menu.Business.Modules.Count > 0)
            {
                for (int i = 0; i < menu.Business.Modules.Count; i++)
                {
                    ProjectMenuModGroup group = menu.Business.Modules[i];
                    if (group.Module != null && group.Module.Count > 0)
                    {
                        for (int k = 0; k < group.Module.Count; k++)
                        {
                            ProjectMenuCommonElement it = group.Module[k];
                            SelectItem item = new SelectItem(it.Command, it.Caption, ProjectUtil.TYPE_BILL_MODULE,group.Caption);
                            CheckedListBoxItem Lit = new CheckedListBoxItem(item, false);
                            ListBoxPop.Items.Add(Lit);
                        }
                    }

                }
            }
        }

        private void ListBox_Click(object sender, EventArgs e)
        {

        }

        private void PopEditCtrl_Closed(object sender, ClosedEventArgs e)
        {
            formatModList.Clear();
            StringBuilder text = new StringBuilder();
            for (int i = 0; i < ListBoxPop.Items.Count; i++)
            {
                CheckedListBoxItem item = (CheckedListBoxItem)ListBoxPop.Items[i];
                if (item.CheckState == CheckState.Checked)
                {
                    SelectItem data = (SelectItem)item.Value;
                    if (text.Length > 0) text.Append(",");
                    text.Append(data.Caption);
                    formatModList.Add(data);
                }
            }
            if (text.Length > 0)
            {
                PopEditCtrl.Text = text.ToString();
            }
            else
            {
                PopEditCtrl.Text = "请选择...";
            }
        }

        public List<SelectItem> FormatModList
        {
            get
            {
                return formatModList;
            }

            set
            {
                formatModList = value;
            }
        }


        private bool isMouseDown = false;
        private Point FormLocation;     //form的location
        private Point mouseOffset;      //鼠标的按下位置

        private void AutoQuerySelect_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = true;
                FormLocation = this.Location;
                mouseOffset = Control.MousePosition;
            }
        }

        private void AutoQuerySelect_MouseMove(object sender, MouseEventArgs e)
        {
            int _x = 0;
            int _y = 0;
            if (isMouseDown)
            {
                Point pt = Control.MousePosition;
                _x = mouseOffset.X - pt.X;
                _y = mouseOffset.Y - pt.Y;

                this.Location = new Point(FormLocation.X - _x, FormLocation.Y - _y);
            }
        }

        private void AutoQuerySelect_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
        }
    }

    public class SelectItem
    {
        private int modType = ProjectUtil.TYPE_DIC_MODULE;
        private string group = "";
        private string caption = "";
        private string key = "";

        private string tableName = "";
        private int nodeType = 0;

        private bool hasRole = false;

        private BillSourceTabItems value = null;

        private BillFormatModule bill = null;

        private bool mainTable = false;

        private List<SelectJoinItem> joinList = new List<SelectJoinItem>();

        public SelectItem() { }

        public SelectItem(string key, string caption, int _type,string gCaption)
        {
            this.key = key;
            this.caption = caption;
            this.modType = _type;
            group = gCaption;
        }

        public SelectItem(BillFormatModule bean, BillSourceTabItems _value, string _caption)
        {
            bill = bean;
            value = _value;

            key = bean.getKey();
            caption = _caption;
            modType = bill.getBillType();
            mainTable = value.Key == "0";
            tableName = value.TableName;
        }

        public int ModType
        {
            get
            {
                return modType;
            }

            set
            {
                modType = value;
            }
        }

        public string Caption
        {
            get
            {
                return caption;
            }

            set
            {
                caption = value;
            }
        }

        public string Key
        {
            get
            {
                return key;
            }

            set
            {
                key = value;
            }
        }

        public int NodeType
        {
            get
            {
                return nodeType;
            }

            set
            {
                nodeType = value;
            }
        }

        public BillSourceTabItems Value
        {
            get
            {
                return value;
            }

            set
            {
                this.value = value;
            }
        }

        public BillFormatModule Bill
        {
            get
            {
                return bill;
            }

            set
            {
                bill = value;
            }
        }

        public bool MainTable
        {
            get
            {
                return mainTable;
            }

            set
            {
                mainTable = value;
            }
        }

        public string TableName
        {
            get
            {
                return tableName;
            }

            set
            {
                tableName = value;
            }
        }

        public List<SelectJoinItem> JoinList
        {
            get
            {
                return joinList;
            }

            set
            {
                joinList = value;
            }
        }

        public bool HasRole
        {
            get
            {
                return hasRole;
            }

            set
            {
                hasRole = value;
            }
        }

        public override string ToString()
        {
            return string.Format("[{0}].[{1}{2}]", (modType == ProjectUtil.TYPE_DIC_MODULE ? "字典" : "单据"),  (group==""?"":group+"."),this.caption);
        }
    }

    //外联设置
    public class SelectJoinItem {

        public const string LEFT = "left join";

        public const string RIGHT = "right join";

        private string joinType = "";

        private SelectItem joinTable = null;

        private string content = "";

        private SelectItem parentTable = null;


        public SelectJoinItem()
        {

        }


        public SelectJoinItem(SelectItem joinTable, string joinType, string sql)
        {
            this.joinTable = joinTable;
            this.joinType = joinType;
            this.content = sql;
        }

        public string JoinType
        {
            get
            {
                return joinType;
            }

            set
            {
                joinType = value;
            }
        }

        public string Content
        {
            get
            {
                return content;
            }

            set
            {
                content = value;
            }
        }

        public SelectItem JoinTable
        {
            get
            {
                return joinTable;
            }

            set
            {
                joinTable = value;
            }
        }

        public SelectItem ParentTable
        {
            get
            {
                return parentTable;
            }

            set
            {
                parentTable = value;
            }
        }
    }
}